using Castle.Components.DictionaryAdapter;
using Castle.MonoRail.Framework;
using KenEgozi.Com.Weblog.Helpers;
using KenEgozi.Com.Weblog.Interfaces;
using KenEgozi.Com.Weblog.Services;

namespace KenEgozi.Com.Weblog.Controllers
{
	[Layout("Default")]
	[Helper(typeof(DateTimeHelper))]
	[Helper(typeof(DivHelper))]
	public abstract class AbstractController : SmartDispatcherController
	{
		readonly ICachedItemsService cachedItemsService;
		readonly IDictionaryAdapterFactory dictionaryAdapterFactory;

		IDefaultLayout layoutBag;
		IRightPanel rightPanelBag;

		public AbstractController(ICachedItemsService cachedItemsService, IDictionaryAdapterFactory dictionaryAdapterFactory)
		{
			this.cachedItemsService = cachedItemsService;
			this.dictionaryAdapterFactory = dictionaryAdapterFactory;
		}

		protected IDefaultLayout LayoutBag
		{
			get
			{
				if (layoutBag == null)
					layoutBag = dictionaryAdapterFactory.GetAdapter<IDefaultLayout>(PropertyBag);
				return layoutBag;
			}
		}

		protected IRightPanel RightPanelBag
		{
			get
			{
				if (rightPanelBag == null)
					rightPanelBag = dictionaryAdapterFactory.GetAdapter<IRightPanel>(PropertyBag);
				return rightPanelBag;
			}
		}

		protected ICachedItemsService CachedItemsService
		{
			get { return cachedItemsService; }
		}

		protected IDictionaryAdapterFactory DictionaryAdapterFactory
		{
			get { return dictionaryAdapterFactory; }
		}

		protected virtual void SetRightPanelParameters()
		{
			RightPanelBag.Archive = CachedItemsService.GetArchive();
			RightPanelBag.Tags = CachedItemsService.GetTagCloud();
			RightPanelBag.Blogroll = CachedItemsService.GetBlogroll();
			RightPanelBag.Stats = CachedItemsService.Stats;
		}

		protected virtual void SetGoogleAnalyticsParameter()
		{
			LayoutBag.ShowGoogleAnalytics = Request.IsLocal == false;
		}

		protected virtual void SetLayoutParameters()
		{
			SetRightPanelParameters();
			SetGoogleAnalyticsParameter();
		}
	}
}
